package com.youkeyi.ddy.cloud.application.business.hr.employee.employee.controller;

import com.sun.xml.bind.v2.TODO;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.BusinessEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.dto.BusinessEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.dto.BusinessEmployeeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.param.*;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.interview.dto.BusinessEmployeeInterviewRecordsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.HrEmployeeInterviewRecordsCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.dto.HrEmployeeInterviewRecordsListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.dto.HrEmployeeInterviewRecordsListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.interview.param.HrEmployeeInterviewRecordsQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.PublicAnnexCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.dto.PublicAnnexListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.dto.PublicAnnexListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.param.PublicAnnexQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.PublicPositionCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.dto.PublicPositionListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.dto.PublicPositionListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.param.PublicPositionQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.PublicUserCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.InterviewStatusEnum;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * BusinessEmployeeController
 *
 * @Author liuxiawang
 * @Date 2024/4/29 10:33 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeController implements BusinessEmployeeCloudService {

    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;
    @Autowired
    private PublicUserCloudService publicUserCloudService;
    @Autowired
    private PublicPositionCloudService publicPositionCloudService;
    @Autowired
    private PublicAnnexCloudService publicAnnexCloudService;
    @Autowired
    private HrEmployeeInterviewRecordsCloudService hrEmployeeInterviewRecordsCloudService;

    @Override
    public BusinessEmployeeListCloudDto list(BusinessEmployeeListCloudParam param) {
        BusinessEmployeeListCloudDto dto = new BusinessEmployeeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        // 先用面试信息，去查询员工先
        if (StringUtils.isNotBlank(param.getInterviewDepartmentKey()) ||
                (null != param.getInterviewDateBegin() && null != param.getInterviewDateEnd())) {
            HrEmployeeInterviewRecordsQueryCloudParam interviewRecordsQueryCloudParam = new HrEmployeeInterviewRecordsQueryCloudParam();
            interviewRecordsQueryCloudParam.setInterviewDepartmentKey(param.getInterviewDepartmentKey());
            interviewRecordsQueryCloudParam.setInterviewDateBegin(param.getInterviewDateBegin());
            interviewRecordsQueryCloudParam.setInterviewDateEnd(param.getInterviewDateEnd());
            interviewRecordsQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
            HrEmployeeInterviewRecordsListCloudDto interviewDtos = hrEmployeeInterviewRecordsCloudService.query(interviewRecordsQueryCloudParam);
            if (null == interviewDtos || CollectionUtils.isEmpty(interviewDtos.getRows())) {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }

            param.setEmployeePrimaryKeys(interviewDtos.getRows().stream().map(HrEmployeeInterviewRecordsListItemCloudDto::getEmployeeKey).distinct().collect(Collectors.toList()));
        }

        HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        employeeQueryCloudParam.setEmployeeCodes(param.getEmployeeCodes());
        employeeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        employeeQueryCloudParam.setPhones(param.getPhones());
        employeeQueryCloudParam.setPhoneFuzzy(param.getPhoneFuzzy());
        employeeQueryCloudParam.setEmployeeStatuses(param.getEmployeeStatuses());
        employeeQueryCloudParam.setEntryDateBegin(param.getEntryDateBegin());
        employeeQueryCloudParam.setEntryDateEnd(param.getEntryDateEnd());
        employeeQueryCloudParam.setRegularizationDateBegin(param.getRegularizationDateBegin());
        employeeQueryCloudParam.setRegularizationDateEnd(param.getRegularizationDateEnd());
        employeeQueryCloudParam.setConstellations(param.getConstellations());
        employeeQueryCloudParam.setZodiacs(param.getZodiacs());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            // 查询岗位
            PublicPositionQueryCloudParam positionQueryCloudParam = new PublicPositionQueryCloudParam();
            positionQueryCloudParam.setPositionPrimaryKeys(employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getPositionKey).collect(Collectors.toList()));
            positionQueryCloudParam.setNeedOrganization(Boolean.TRUE);
            PublicPositionListCloudDto positionDtos = publicPositionCloudService.query(positionQueryCloudParam);
            Map<String, PublicPositionListItemCloudDto> positionMap = new HashMap<>();
            if (null != positionDtos && CollectionUtils.isNotEmpty(positionDtos.getRows())) {
                positionMap.putAll(positionDtos.getRows().stream().collect(Collectors.toMap(PublicPositionListItemCloudDto::getPositionPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            HrEmployeeInterviewRecordsQueryCloudParam interviewRecordsQueryCloudParam = new HrEmployeeInterviewRecordsQueryCloudParam();
            interviewRecordsQueryCloudParam.setEmployeePrimaryKeys(employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getEmployeePrimaryKey).collect(Collectors.toList()));
            HrEmployeeInterviewRecordsListCloudDto interviewDtos = hrEmployeeInterviewRecordsCloudService.query(interviewRecordsQueryCloudParam);
            Map<String, List<HrEmployeeInterviewRecordsListItemCloudDto>> interviewMaps = new HashMap<>();
            if (null != interviewDtos && CollectionUtils.isNotEmpty(interviewDtos.getRows())) {
                interviewMaps.putAll(interviewDtos.getRows().stream().collect(Collectors.groupingBy(HrEmployeeInterviewRecordsListItemCloudDto::getEmployeeKey)));
            }

            List<String> profilePictureKeys = employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getProfilePictureKey).collect(Collectors.toList());
            Map<String, String> pictureMap = new HashMap<>();
            PublicAnnexQueryCloudParam annexQueryCloudParam = new PublicAnnexQueryCloudParam();
            annexQueryCloudParam.setAnnexPrimaryKeys(profilePictureKeys);
            PublicAnnexListCloudDto annexDtos = publicAnnexCloudService.query(annexQueryCloudParam);
            if (null != annexDtos && CollectionUtils.isNotEmpty(annexDtos.getRows())) {
                pictureMap.putAll(annexDtos.getRows().stream().collect(Collectors.toMap(PublicAnnexListItemCloudDto::getAnnexPrimaryKey, PublicAnnexListItemCloudDto::getUrl, (k1, k2) -> k1)));
            }

            List<BusinessEmployeeListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeeListItemCloudDto item = new BusinessEmployeeListItemCloudDto();
                item.setEmployeePrimaryKey(employee.getPrimaryKey());
                item.setPrimaryKey(employee.getPrimaryKey());
                item.setEmployeeCode(employee.getEmployeeCode());
                item.setName(employee.getName());
                item.setFormerName(employee.getFormerName());
                item.setIdCard(employee.getIdCard());
                item.setSex(employee.getSex());
                item.setBirthday(employee.getBirthday());
                item.setZodiac(employee.getZodiac());
                item.setConstellation(employee.getConstellation());
                item.setNationality(employee.getNationality());
                item.setNativePlace(employee.getNativePlace());
                item.setNickName(employee.getNickName());
                item.setProvince(employee.getProvince());
                item.setCity(employee.getCity());
                item.setArea(employee.getArea());
                item.setCurrentResidence(employee.getCurrentResidence());
                item.setMaritalStatus(employee.getMaritalStatus());
                item.setPhone(employee.getPhone());
                item.setEmail(employee.getEmail());
                item.setProfilePictureKey(employee.getProfilePictureKey());
                item.setProfilePictureUrl(pictureMap.get(item.getProfilePictureKey()));
                item.setPoliticalStatus(employee.getPoliticalStatus());
                item.setJoinLeagueDate(employee.getJoinLeagueDate());
                item.setJoinPartyDate(employee.getJoinPartyDate());
                item.setHighestEducation(employee.getHighestEducation());
                item.setHighestDegree(employee.getHighestDegree());
                item.setStartWorkDate(employee.getStartWorkDate());
                item.setEmergencyContact(employee.getEmergencyContact());
                item.setEmergencyContactPhone(employee.getEmergencyContactPhone());
                item.setOtherContact(employee.getOtherContact());
                item.setAccount(employee.getAccount());
                item.setDepartmentKey(employee.getDepartmentKey());
                item.setPositionKey(employee.getPositionKey());
                item.setOfficeLocation(employee.getOfficeLocation());
                item.setOfficePhone(employee.getOfficePhone());
                item.setEmployeeStatus(employee.getEmployeeStatus());
                item.setEmployeeStatusName(employee.getEmployeeStatusName());
                item.setJobNature(employee.getJobNature());
                item.setEntryDate(employee.getEntryDate());
                item.setPlanEntryDate(employee.getPlanEntryDate());
                item.setEntryChannel(employee.getEntryChannel());
                item.setEntryChannelRecommendedBy(employee.getEntryChannelRecommendedBy());
                item.setProbationPeriod(employee.getProbationPeriod());
                item.setRegularizationDate(employee.getRegularizationDate());
                item.setAttachmentKey(employee.getAttachmentKey());
                item.setResignationApplicationDate(employee.getResignationApplicationDate());
                item.setResignationActualDate(employee.getResignationActualDate());
                item.setResignationSettlementDate(employee.getResignationSettlementDate());
                item.setResignationReason(employee.getResignationReason());
                item.setResignationDestination(employee.getResignationDestination());
                item.setResignationRemark(employee.getResignationRemark());
                item.setRemark(employee.getRemark());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                if (positionMap.containsKey(employee.getPositionKey())) {
                    PublicPositionListItemCloudDto position = positionMap.get(employee.getPositionKey());
                    item.setPositionName(position.getName());
                    if (null != position.getOrganization()) {
                        item.setDepartmentName(position.getOrganization().getName());
                    }
                }
                if (interviewMaps.containsKey(employee.getPrimaryKey())) {
                    List<HrEmployeeInterviewRecordsListItemCloudDto> interviewRecords = interviewMaps.get(employee.getEmployeePrimaryKey());
                    if (CollectionUtils.isNotEmpty(interviewRecords)&&interviewRecords.size()>0) {
                        //将面试记录 存放在 interviewRecordsItems
                        List<BusinessEmployeeInterviewRecordsListItemCloudDto> itemInterviewRecords=interviewRecords.stream().map(x->{
                            BusinessEmployeeInterviewRecordsListItemCloudDto businessDto=new BusinessEmployeeInterviewRecordsListItemCloudDto();
                            BeanUtils.copyProperties(x,businessDto);
                            return businessDto;
                        }).collect(Collectors.toList());
                        item.setInterviewRecordsItems(itemInterviewRecords);
                        // 先将数据 根据时间降序
                        List<HrEmployeeInterviewRecordsListItemCloudDto> sortedInterviewRecords = interviewRecords.stream()
                                .sorted(Comparator.comparing(HrEmployeeInterviewRecordsListItemCloudDto::getCreateTime).reversed())
                                .collect(Collectors.toList());

                        setHrEmployeeInterviewRecordsGiveEmployeeDto(sortedInterviewRecords.get(0),item);//将面试记录最新的 赋值到员工基础信息中

                        final String[] interviewerInfo = {null};
                        interviewRecords.forEach(interview ->
                                interviewerInfo[0] = String.format("%s    %s    %s    %s    %s; ",
                                        "面试日期:"+(interview.getStartDate() != null ? SystemDateUtils.ofPattern(interview.getStartDate(), "yyyy-MM-dd") : null),
                                        "面试时间:"+interview.getInterviewTime(),
                                        "面试部门:"+interview.getDepartmentName(),
                                        "面试岗位:"+interview.getPositionName(),
                                        "面试状态:"+ InterviewStatusEnum.fromCode(Integer.parseInt(interview.getInterviewerStatus())).getDesc()));
                        item.setInterviewInfo(interviewerInfo[0]);
                    }
                }
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeeCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        String userKey = null;
        if (StringUtils.isNotBlank(param.getPhone())) {
            PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
            userQueryCloudParam.setPhones(Collections.singletonList(param.getPhone()));
            PublicUserListCloudDto userDtos = publicUserCloudService.query(userQueryCloudParam);
            if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
                userKey = userDtos.getRows().get(0).getUserPrimaryKey();
            }
        }

        HrEmployeeCreateCloudParam employeeCreateCloudParam = new HrEmployeeCreateCloudParam();
        if (StringUtils.isNotBlank(userKey)) {
            employeeCreateCloudParam.setEmployeePrimaryKey(userKey);
        }
        employeeCreateCloudParam.setCorpKey(corpKey);
        employeeCreateCloudParam.setEmployeeCode(param.getEmployeeCode());
        employeeCreateCloudParam.setName(param.getName());
        employeeCreateCloudParam.setFormerName(param.getFormerName());
        employeeCreateCloudParam.setIdCard(param.getIdCard());
        employeeCreateCloudParam.setSex(param.getSex());
        employeeCreateCloudParam.setBirthday(param.getBirthday());
        employeeCreateCloudParam.setZodiac(param.getZodiac());
        employeeCreateCloudParam.setNationality(param.getNationality());
        employeeCreateCloudParam.setNativePlace(param.getNativePlace());
        employeeCreateCloudParam.setNickName(param.getNickName());
        employeeCreateCloudParam.setProvince(param.getProvince());
        employeeCreateCloudParam.setCity(param.getCity());
        employeeCreateCloudParam.setArea(param.getArea());
        employeeCreateCloudParam.setCurrentResidence(param.getCurrentResidence());
        employeeCreateCloudParam.setMaritalStatus(param.getMaritalStatus());
        employeeCreateCloudParam.setPhone(param.getPhone());
        employeeCreateCloudParam.setEmail(param.getEmail());
        employeeCreateCloudParam.setProfilePictureKey(param.getProfilePictureKey());
        employeeCreateCloudParam.setPoliticalStatus(param.getPoliticalStatus());
        employeeCreateCloudParam.setJoinLeagueDate(param.getJoinLeagueDate());
        employeeCreateCloudParam.setJoinPartyDate(param.getJoinPartyDate());
        employeeCreateCloudParam.setHighestEducation(param.getHighestEducation());
        employeeCreateCloudParam.setHighestDegree(param.getHighestDegree());
        employeeCreateCloudParam.setStartWorkDate(param.getStartWorkDate());
        employeeCreateCloudParam.setEmergencyContact(param.getEmergencyContact());
        employeeCreateCloudParam.setEmergencyContactPhone(param.getEmergencyContactPhone());
        employeeCreateCloudParam.setOtherContact(param.getOtherContact());
        employeeCreateCloudParam.setAccount(param.getPhone());
        employeeCreateCloudParam.setDepartmentKey(param.getDepartmentKey());
        employeeCreateCloudParam.setPositionKey(param.getPositionKey());
        employeeCreateCloudParam.setOfficeLocation(param.getOfficeLocation());
        employeeCreateCloudParam.setOfficePhone(param.getOfficePhone());
        employeeCreateCloudParam.setEmployeeStatus(param.getEmployeeStatus());
        employeeCreateCloudParam.setJobNature(param.getJobNature());
        employeeCreateCloudParam.setEntryDate(param.getEntryDate());
        employeeCreateCloudParam.setPlanEntryDate(param.getPlanEntryDate());
        employeeCreateCloudParam.setEntryChannel(param.getEntryChannel());
        employeeCreateCloudParam.setEntryChannelRecommendedBy(param.getEntryChannelRecommendedBy());
        employeeCreateCloudParam.setProbationPeriod(param.getProbationPeriod());
        employeeCreateCloudParam.setRegularizationDate(param.getRegularizationDate());
        employeeCreateCloudParam.setAttachmentKey(param.getAttachmentKey());
        employeeCreateCloudParam.setResignationApplicationDate(param.getResignationApplicationDate());
        employeeCreateCloudParam.setResignationActualDate(param.getResignationActualDate());
        employeeCreateCloudParam.setResignationSettlementDate(param.getResignationSettlementDate());
        employeeCreateCloudParam.setResignationReason(param.getResignationReason());
        employeeCreateCloudParam.setResignationDestination(param.getResignationDestination());
        employeeCreateCloudParam.setResignationRemark(param.getResignationRemark());
        employeeCreateCloudParam.setRemark(param.getRemark());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        rc = hrEmployeeCloudService.create(employeeCreateCloudParam);
        if (null != rc && rc.isSuccess()) {
            if (param.getNeedCreateAccount() && StringUtils.isNotBlank(param.getPhone()) && StringUtils.isBlank(userKey)) {
                PublicUserCreateCloudParam userCreateCloudParam = new PublicUserCreateCloudParam();
                userCreateCloudParam.setUserPrimaryKey(employeeCreateCloudParam.getEmployeePrimaryKey());
                userCreateCloudParam.setCorpKey(employeeCreateCloudParam.getCorpKey());
                userCreateCloudParam.setName(employeeCreateCloudParam.getName());
                userCreateCloudParam.setNickName(employeeCreateCloudParam.getName());
                userCreateCloudParam.setPhone(employeeCreateCloudParam.getPhone());
                userCreateCloudParam.setIsRoot(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
                userCreateCloudParam.setEmail(employeeCreateCloudParam.getEmail());
                userCreateCloudParam.setPassWord(param.getPassword());
                userCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                userCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
                return publicUserCloudService.create(userCreateCloudParam);
            } else if (StringUtils.isNotBlank(userKey)) {
                PublicUserUpdateCloudParam userUpdateCloudParam = new PublicUserUpdateCloudParam();
                userUpdateCloudParam.setUserPrimaryKey(userKey);
                userUpdateCloudParam.setPhone(employeeCreateCloudParam.getPhone());
                publicUserCloudService.update(userUpdateCloudParam);
            }
        }
        return rc;
    }

    @Override
    public ResponseContent update(BusinessEmployeeUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeePrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeePrimaryKey(param.getEmployeePrimaryKey());
        employeeUpdateCloudParam.setEmployeeCode(param.getEmployeeCode());
        employeeUpdateCloudParam.setName(param.getName());
        employeeUpdateCloudParam.setFormerName(param.getFormerName());
        employeeUpdateCloudParam.setIdCard(param.getIdCard());
        employeeUpdateCloudParam.setSex(param.getSex());
        employeeUpdateCloudParam.setBirthday(param.getBirthday());
        employeeUpdateCloudParam.setZodiac(param.getZodiac());
        employeeUpdateCloudParam.setConstellation(param.getConstellation());
        employeeUpdateCloudParam.setNationality(param.getNationality());
        employeeUpdateCloudParam.setNativePlace(param.getNativePlace());
        employeeUpdateCloudParam.setNickName(param.getNickName());
        employeeUpdateCloudParam.setProvince(param.getProvince());
        employeeUpdateCloudParam.setCity(param.getCity());
        employeeUpdateCloudParam.setArea(param.getArea());
        employeeUpdateCloudParam.setCurrentResidence(param.getCurrentResidence());
        employeeUpdateCloudParam.setMaritalStatus(param.getMaritalStatus());
        employeeUpdateCloudParam.setPhone(param.getPhone());
        employeeUpdateCloudParam.setEmail(param.getEmail());
        employeeUpdateCloudParam.setProfilePictureKey(param.getProfilePictureKey());
        employeeUpdateCloudParam.setPoliticalStatus(param.getPoliticalStatus());
        employeeUpdateCloudParam.setJoinLeagueDate(param.getJoinLeagueDate());
        employeeUpdateCloudParam.setJoinPartyDate(param.getJoinPartyDate());
        employeeUpdateCloudParam.setHighestEducation(param.getHighestEducation());
        employeeUpdateCloudParam.setHighestDegree(param.getHighestDegree());
        employeeUpdateCloudParam.setStartWorkDate(param.getStartWorkDate());
        employeeUpdateCloudParam.setEmergencyContact(param.getEmergencyContact());
        employeeUpdateCloudParam.setEmergencyContactPhone(param.getEmergencyContactPhone());
        employeeUpdateCloudParam.setOtherContact(param.getOtherContact());
        employeeUpdateCloudParam.setAccount(param.getPhone());
        employeeUpdateCloudParam.setDepartmentKey(param.getDepartmentKey());
        employeeUpdateCloudParam.setPositionKey(param.getPositionKey());
        employeeUpdateCloudParam.setOfficeLocation(param.getOfficeLocation());
        employeeUpdateCloudParam.setOfficePhone(param.getOfficePhone());
        employeeUpdateCloudParam.setEmployeeStatus(param.getEmployeeStatus());
        employeeUpdateCloudParam.setJobNature(param.getJobNature());
        employeeUpdateCloudParam.setEntryDate(param.getEntryDate());
        employeeUpdateCloudParam.setPlanEntryDate(param.getPlanEntryDate());
        employeeUpdateCloudParam.setEntryChannel(param.getEntryChannel());
        employeeUpdateCloudParam.setEntryChannelRecommendedBy(param.getEntryChannelRecommendedBy());
        employeeUpdateCloudParam.setProbationPeriod(param.getProbationPeriod());
        employeeUpdateCloudParam.setRegularizationDate(param.getRegularizationDate());
        employeeUpdateCloudParam.setAttachmentKey(param.getAttachmentKey());
        employeeUpdateCloudParam.setResignationApplicationDate(param.getResignationApplicationDate());
        employeeUpdateCloudParam.setResignationActualDate(param.getResignationActualDate());
        employeeUpdateCloudParam.setResignationSettlementDate(param.getResignationSettlementDate());
        employeeUpdateCloudParam.setResignationReason(param.getResignationReason());
        employeeUpdateCloudParam.setResignationDestination(param.getResignationDestination());
        employeeUpdateCloudParam.setResignationRemark(param.getResignationRemark());
        employeeUpdateCloudParam.setRemark(param.getRemark());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        rc = hrEmployeeCloudService.update(employeeUpdateCloudParam);
        if (null != rc && rc.isSuccess()) {
            if (StringUtils.isNotBlank(param.getPhone())) {
                // 修改这个用户的手机号
                PublicUserUpdateCloudParam userUpdateCloudParam = new PublicUserUpdateCloudParam();
                userUpdateCloudParam.setUserPrimaryKey(param.getEmployeePrimaryKey());
                userUpdateCloudParam.setPhone(param.getPhone());
                publicUserCloudService.update(userUpdateCloudParam);
            }
        }
        return rc;
    }

    @Override
    public ResponseContent delete(BusinessEmployeeDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeePrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeDeleteCloudParam employeeDeleteCloudParam = new HrEmployeeDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeeCloudService.delete(employeeDeleteCloudParam);
    }


    @Override
    public ResponseContent dutyConfirm(BusinessEmployeeDutyConfirmCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeePrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeeUpdateCloudParam employeeUpdateCloudParam = new HrEmployeeUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeePrimaryKey(param.getEmployeePrimaryKey());
        employeeUpdateCloudParam.setEmployeeCode(param.getEmployeeCode());
        employeeUpdateCloudParam.setPhone(param.getPhone());
        employeeUpdateCloudParam.setAccount(param.getPhone());
        employeeUpdateCloudParam.setDepartmentKey(param.getDepartmentKey());
        employeeUpdateCloudParam.setPositionKey(param.getPositionKey());
        employeeUpdateCloudParam.setEmployeeStatus(param.getEmployeeStatus());
        employeeUpdateCloudParam.setJobNature(param.getJobNature());
        employeeUpdateCloudParam.setEntryDate(param.getEntryDate());
        employeeUpdateCloudParam.setPlanEntryDate(param.getPlanEntryDate());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        rc = hrEmployeeCloudService.update(employeeUpdateCloudParam);
        if (null != rc && rc.isSuccess()) {
            if (param.getNeedCreateAccount() && StringUtils.isNotBlank(param.getPhone())) {
                // 查询员工数据
                HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
                employeeQueryCloudParam.setEmployeePrimaryKeys(Collections.singletonList(param.getEmployeePrimaryKey()));
                HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
                if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
                    HrEmployeeListItemCloudDto employee = employeeDtos.getRows().get(0);
                    PublicUserCreateCloudParam userCreateCloudParam = new PublicUserCreateCloudParam();
                    userCreateCloudParam.setUserPrimaryKey(employee.getEmployeePrimaryKey());
                    userCreateCloudParam.setCorpKey(employee.getCorpKey());
                    userCreateCloudParam.setName(employee.getName());
                    userCreateCloudParam.setNickName(employee.getName());
                    userCreateCloudParam.setPhone(employee.getPhone());
                    userCreateCloudParam.setIsRoot(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
                    userCreateCloudParam.setEmail(employee.getEmail());
                    userCreateCloudParam.setPassWord(param.getPassword());
                    userCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                    userCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
                    return publicUserCloudService.create(userCreateCloudParam);
                }
            }
        }
        return rc;
    }

    private void setHrEmployeeInterviewRecordsGiveEmployeeDto(HrEmployeeInterviewRecordsListItemCloudDto hrdto,BusinessEmployeeListItemCloudDto dto){
        dto.setInterviewComments(hrdto.getInterviewComments());
        dto.setInterviewDepartmentName(hrdto.getDepartmentName());
        dto.setInterviewPositionName(hrdto.getPositionName());
        dto.setInterviewerName(hrdto.getInterviewerName());
        dto.setInterviewerStatus(hrdto.getInterviewerStatus());
        dto.setInterviewTime(hrdto.getInterviewTime());
        dto.setStartDate(hrdto.getStartDate());
    }
}
